var swiperData;
var bannerData;
var categoryData;
var goodsList;
var totalPrice = 0;
var sum = 0;
angular.module("myApp")
	.factory("CommonService", ["$http", function($http) {
		goodsList = [
			
		];
		function getTotalPirce() {
			for (var item of goodsList) {
				totalPrice += Number(item.price) * item.num;
				totalPrice = Number(totalPrice.toFixed(1));
			}
		}
		function marketNum() {
			sum = 0;
			for (item of goodsList) {
				sum += item.num;
			}
			if (sum == 0) {
				$(".addNum").css("display", "none");
			}
			$(".addNum").html(sum);
		}
		return {
			//根据分类请求数据
			getCategoryData: function(category) {
				$http.get("http://www.vrserver.applinzi.com/aixianfeng/apicategory.php?category=" + category)
					.success(function(result) {
						categoryData = result.data;
						//如果没有数据，则默认为热销榜
						if (categoryData == undefined) {
							$http.get("http://www.vrserver.applinzi.com/aixianfeng/apicategory.php?category=热销榜")
								.success(function(result) {
									categoryData = result.data;
								})
						}
					})
				return categoryData;
			},
			//market页面：+ - 号添加商品
			addShopping: function(key, sign, price) {
				var flag;
				sign == 1 ? flag = 1 : flag = -1;
				for (var i in goodsList) {
					if (goodsList[i].key == key) {
						goodsList[i].num += flag;
						$(document.getElementsByName(key)[0]).css("display", "block");
						$(".addNum").css("display", "block");
						totalPrice += Number(price) * flag;
						totalPrice = Number(totalPrice.toFixed(1));
						marketNum();
						if (goodsList[i].num <=0) {
							$(document.getElementsByName(key)[0]).css("display", "none");
							goodsList.splice(i, 1);
						}
						return;
					}
				}
				//第一次添加
				for (var item of categoryData) {
					if (item.$$hashKey == key) {
						goodsList.push(
							{ 
								"key": item.$$hashKey,
								"name": item.name,
								"img": item.img,
								"price": item.price,
								"num": 1
							}
						);
						$(document.getElementsByName(key)[0]).css("display", "block");
						$(".addNum").css("display", "block");
						totalPrice += Number(price);
						totalPrice = Number(totalPrice.toFixed(1));
						marketNum();
						return;
					}
				}
			},
			getNum: function(key) {
				for (var item of goodsList) {
					if (key == item.key) {
						return item.num;
					}
				}
			},
			//cart页面：获取添加的商品信息
			getShopping: function() {
				return goodsList;
			},
			//cart页面： 计算商品的总价钱
			getTotalPirce: function() {
				return totalPrice;
			},
			//cart页面：选择框
			check: function(index, price, num) {
				$($(".order-list .choice")[index]).toggleClass("choice2");
				$($(".order-list .choice")[index]).hasClass("choice2") ? a = -1 : a = 1;
				totalPrice += Number(price) * num * a;
				totalPrice = Number(totalPrice.toFixed(1));
				sum += num * a;
				$(".addNum").html(sum);
			},
			//cart页面：+ - 添加或减少数量
			addAndSub: function(key, price, sign) {
				for (var i in goodsList) {
					if (key == goodsList[i].key) {
						totalPrice += Number(price) * sign;
						totalPrice = Number(totalPrice.toFixed(1));
						goodsList[i].num += sign;
						marketNum();
						if (goodsList[i].num == 0) {
							goodsList.splice(i, 1);
							return goodsList;
						}
						return;
					}
				}
			}
			
		}
	}])
	.factory("HomeService", ["$http", function($http) {
		$http.get("http://www.vrserver.applinzi.com/aixianfeng/apihome.php")
			.success(function(result) {
				swiperData = result.data.slide;
				bannerData = result.data.menu;
			});
			
		return {
			//home页面： 轮播图
			getImg: function() {
				return swiperData;
			},
			//home页面：分类数据
			getBannerData: function() {
				return bannerData;
			}
		}
	}])
	.factory("DetailService", ["$http", function($http) {
		return {
			//详情页
			getDetailData: function(key) {
				for ( var item of categoryData) {
					if (item.$$hashKey == key) {
						return item;
					}
				}
			}
		}
	}])




